package com.lil.qmui.manager;

import android.content.Context;
import android.content.SharedPreferences;

import com.lil.qmui.app.MyApp;

/**
 * @author 作者： Administrator
 * 用于保存登录用户相关信息
 * @version 创建时间：2014年8月26日下午6:54:04
 */
public class UserSPManager {

    public static String PREFS_NAME = "spring_boot";

    public static SharedPreferences sp;
    public static SharedPreferences.Editor editor;
    public static Context context = null;
    public static String code = "code";

    public static String SHARED_KEY_USERNAME = "shared_key_username";// 用户名
    public static String USER_ID = "user_id";// 用户id 同 手机号
    public static String USER_STATE = "user_state";// 用户登录状态
    public static String TOKEN = "token";// 用户token 包含了有效期
    public static String HEADERFILE = "header";// 用户头像

    /**
     * SharedPreferences初始化，每次读写操作都必须
     */
    public static void initPreference() {
        if (context == null) {
            context = MyApp.getContext();
        }
        sp = context.getSharedPreferences(PREFS_NAME,
                Context.MODE_PRIVATE);
        editor = sp.edit();
        editor.commit();
    }

    public static String getSharedPreference(String name, String delfultValue) {
        initPreference();
        return sp.getString(name, delfultValue);
    }

    public static boolean getSharedPreference(String name,
                                              boolean delfultValue) {
        initPreference();
        return sp.getBoolean(name, delfultValue);
    }

    public static int getSharedPreference(String name, int defaultValue) {
        initPreference();
        return sp.getInt(name, defaultValue);
    }

    public static void setSharedPreference(String name, boolean value) {
        initPreference();
        editor.putBoolean(name, value);
        editor.commit();
    }

    public static void setSharedPreference(String name, int value) {
        initPreference();
        editor.putInt(name, value);
        editor.commit();
    }

    /**
     * @param token
     */
    public static void setToken(String token) {
        initPreference();
        editor.putString(TOKEN, token);
        editor.commit();
    }

    public static String getToken() {
        initPreference();
        return sp.getString(TOKEN, "");// 设置初始值
    }

    /**
     * @param user_name
     */
    public static void setUserName(String user_name) {
        initPreference();
        editor.putString(SHARED_KEY_USERNAME, user_name);
        editor.commit();
    }

    public static String getUserName() {
        initPreference();
        return sp.getString(SHARED_KEY_USERNAME, "");// 设置初始值
    }

    /**
     * 头像
     *
     * @param header
     */
    public static void setHeader(String header) {
        initPreference();
        editor.putString(HEADERFILE, header);
        editor.commit();
    }

    public static String getHeader() {
        initPreference();
        return sp.getString(HEADERFILE, "");// 设置初始值
    }

    /**
     * @param user_id
     */
    public static void setUserId(String user_id) {
        initPreference();
        editor.putString(USER_ID, user_id);
        editor.commit();
    }

    public static String getUserId() {
        initPreference();
        return sp.getString(USER_ID, "");// 设置初始值
    }


    public static void delUserName() {
        initPreference();
        editor.remove(SHARED_KEY_USERNAME);
        editor.commit();
    }

    public static void setUserState(String string) {
        initPreference();
        editor.putString(USER_STATE, string);
        editor.commit();
    }

    public static String getUserState() {
        initPreference();
        return sp.getString(USER_STATE, "");// 设置初始值
    }

    public static void delUserState() {
        initPreference();
        editor.remove(USER_STATE);
        editor.commit();
    }


    public static void setSharedPreference(String name, String value) {
        initPreference();
        editor.putString(name, value);
        editor.commit();
    }
}
